import { io, Socket } from "socket.io-client";
import {Message} from "../api/message";


export class SocketIO {
    socket: Socket;

    constructor(url: string) {
        this.socket = io(url);
        this.socket.on('connect', () => {
            console.log('Connected to server');
        });
        this.socket.on('disconnect', () => {
            console.log('Disconnected from server');
        });
    }

    joinRoom(room: string) {
        this.socket.emit('joinRoom', room);
    }

    sendMessage(roomname: string,owner:string, message: string) {
        this.socket.emit('sendMessage', { roomname,owner,message});
    }

    onMessage(callback: (message:string) => void) {
        this.socket.on('message', (message: Message) => {
            console.log(message)

            callback(message.roomname);
        });
    }
}


